﻿

/***************************************
= (document.ready)
-------------------------------------- */
$(function () {
    // Popup Quiz
    $("#popup-close").click(function () {
        disablePopup();
    });

    $("#bg-popup").click(function () {
        disablePopup();
    });
});

/***************************************
= Popup
-------------------------------------- */
var popupStatus = 0;

function showPopup() {
    if (popupStatus == 0) {
        $('#bg-popup').css({ 'opacity': '0.6' });
        $('#bg-popup').fadeIn('slow');
        $('#popup').fadeIn('slow');
        popupStatus = 1;

        var wWidth = document.documentElement.clientWidth;
        var wHeight = document.documentElement.clientHeight;

        var popupWidth = $('#popup').width();
        var popupHeight = $('#popup').height();

        $('#popup').css({ 'position': 'fixed', 'top': wHeight / 2 - popupHeight / 2, 'left': wWidth / 2 - popupWidth / 2 });

        $('#bg-popup').css({ 'height': wHeight }); // only need force for IE6
    }
}

function disablePopup() {
    $('#bg-popup').fadeOut('slow');
    $('#popup').fadeOut('slow');
    popupStatus = 0;
}

// Press Escape Event!
$(document).keypress(function (e) {
    if (e.keyCode == 27 && popupStatus == 1) {
        disablePopup();    
    }

    if (e.keyCode == 27) {
        $('#ecommerce_container').css('display', 'none');
        closePreview();
    }    
});

function closePreview() {
    $('#popup_container').hide();
}

/***************************************
= Get All Rows Have Checked
-------------------------------------- */
//#region 
function selectAllRows(spanChk) {
    var oItem = spanChk.children;
    var theBox = (spanChk.type == "checkbox") ?
            spanChk : spanChk.children.item[0];
    xState = theBox.checked;
    //elm = theBox.form.elements;
    elm = $("#grdList :checkbox");

    for (i = 0; i < elm.length; i++)
        if (elm[i].type == "checkbox" &&
                  elm[i].id != theBox.id) {
            if (elm[i].checked == xState)
                elm[i].click();
        }
}
//#endregion

/***************************************
= Lấy tham số trên URL javascript
-------------------------------------- */
function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.search);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}  

/***************************************
= String Format
-------------------------------------- */
//#region
String.format = function (text) {
    //check if there are two arguments in the arguments list
    if (arguments.length <= 1) {
        //if there are not 2 or more arguments there's nothing to replace
        //just return the original text
        return text;
    }

    //decrement to move to the second argument in the array
    var tokenCount = arguments.length - 2;
    for (var token = 0; token <= tokenCount; token++) {
        //iterate through the tokens and replace their placeholders from the original text in order
        text = text.replace(new RegExp("\\{" + token + "\\}", "gi"),
                                                arguments[token + 1]);
    }
    return text;
};
//#endregion

/***************************************
= Remove Sign
-------------------------------------- */
//#region
function removeSign(str) {
    str = str.toLowerCase();
    str = str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g, "a");
    str = str.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g, "e");
    str = str.replace(/ì|í|ị|ỉ|ĩ/g, "i");
    str = str.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g, "o");
    str = str.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g, "u");
    str = str.replace(/ỳ|ý|ỵ|ỷ|ỹ/g, "y");
    str = str.replace(/đ/g, "d");
    str = str.replace(/!|@|%|\^|\*|\(|\)|\+|\=|\<|\>|\?|\/|,|\.|\:|\;|\'| |\"|\&|\#|\[|\]|~|$|_/g, "-");

    /* tìm và thay thế các kí tự đặc biệt trong chuỗi sang kí tự - */
    str = str.replace(/-+-/g, "-"); //thay thế 2- thành 1-     
    str = str.replace(/^\-+|\-+$/g, "");

    //cắt bỏ ký tự - ở đầu và cuối chuỗi  
    return str;
}
//#endregion

// Replaces all instances of the given substring.
String.prototype.replaceAll = function(
    strTarget, // The substring you want to replace
    strSubString // The string you want to replace in.
) {
    var strText = this;
    var intIndexOfMatch = strText.indexOf(strTarget);

    // Keep looping while an instance of the target string
    // still exists in the string.
    while (intIndexOfMatch != -1) {
        // Relace out the current instance.
        strText = strText.replace(strTarget, strSubString);

        // Get the index of any next matching substring.
        intIndexOfMatch = strText.indexOf(strTarget);
    }

    // Return the updated string with ALL the target strings
    // replaced out with the new substring.
    return (strText);
};
